<?php
class Utils {
  public static function dump($obj, $what = NULL) {
    if (defined ('DEBUG_ENABLED') && !DEBUG_ENABLED) {
      return;
    }
    echo "<pre>\n";
    if (is_null ($obj)) {
      echo ($what !== NULL) ? "$what=!NULL!" : '!NULL!';
    } else if ($obj === '') {
      echo ($what !== NULL) ? "$what=!EMPTY!" : '!EMPTY!';
    } else if ($obj === false) {
      echo ($what !== NULL) ? "$what=!FALSE!" : '!FALSE!';
    } else {
      if ($what !== NULL) {
        echo "$what=<br>";
      }
      echo htmlspecialchars (print_r ($obj, true));
    }
    echo "</pre>\n";
  }

  public static function get_param($itm, $default = '', $trimText = false) {
    if (isset($_POST[$itm])) {
      return urldecode(($trimText) ? trim($_POST[$itm]) : $_POST[$itm]);
    } else if (isset($_GET[$itm])) {
      return urldecode(($trimText) ? trim($_GET[$itm]) : $_GET[$itm]);
    } else {
      return urldecode($default);
    }
  }

  public static function get_url() {
    $pageURL = 'http';
    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
  }
  
  public static function get_short($str, $num) {
    if (strlen($str) <= $num) {
      return $str;
    }
    return mb_substr($str, 0, $num, 'utf-8') . '...';
  }

  public static function convert($str,$ky=''){
    if($ky=='')return $str;
    $ky=str_replace(chr(32),'',$ky);
    if(strlen($ky)<8)exit('key error');
    $kl=strlen($ky)<32?strlen($ky):32;
    $k=array();for($i=0;$i<$kl;$i++){
      $k[$i]=ord($ky{$i})&0x1F;}
      $j=0;for($i=0;$i<strlen($str);$i++){
        $e=ord($str{$i});
        $str{$i}=$e&0xE0?chr($e^$k[$j]):chr($e);
        $j++;$j=$j==$kl?0:$j;}
        return $str;
  }

  public static function rrmdir($dir) {
    if (is_dir($dir)) {
      $objects = scandir($dir);
      foreach ($objects as $object) {
        if ($object != "." && $object != "..") {
          if (filetype($dir . "/". $object) == "dir") {
            Utils::rrmdir($dir . "/". $object);
          } else {
            unlink($dir . "/" . $object);
          }
        }
      }
      reset($objects);
      rmdir($dir);
    }
  }

  public static function create($dir) {
    if (!is_dir($dir)) {
      $temp = explode('/',$dir);
      $cur_dir = '';
      for($i = 0; $i < count($temp); $i++) {
        $cur_dir .= $temp[$i].'/';
        if (!is_dir($cur_dir)) {
          @mkdir($cur_dir, 0777);
        }
      }
    }
  }

  public static function get_inst_cd($inst) {
    return '{' . $inst->__folder . ':' . $inst->id . '}';
  }

  public static function unique($length=16) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
  }

  public static function redirect($url, $is_iframe) {
    if ($is_iframe == 1) {
      echo "<script>parent.location.href = '$url';</script>";
    } else {
      header('location: ' . $url);
    }
  }

  public static function get_requrl($app_data, $delimiter='&') {
    global $__inst, $__file, $__iframe;
    return  '__fw.php?__sys=' . $app_data->sys->php_name . $delimiter . "__file=$__file" . $delimiter . "__inst=$__inst" . $delimiter . "__iframe=$__iframe" . $delimiter . '__md=';
  }
  
  public static function  get_ip(){ 
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
      $ip = getenv("HTTP_CLIENT_IP"); 
    } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
      $ip = getenv("HTTP_X_FORWARDED_FOR"); 
    } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
      $ip = getenv("REMOTE_ADDR"); 
    } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
      $ip = $_SERVER['REMOTE_ADDR']; 
    } else {
      $ip = "unknown"; 
    }
    return($ip);
  }
}
?>